package com.itchen.hospital.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itchen.hospital.exception.CustomException;
import com.itchen.hospital.mapper.AdministrativeMapper;
import com.itchen.hospital.pojo.Administrative;
import com.itchen.hospital.pojo.Params;
import com.itchen.hospital.service.AdministrativeService;
import com.itchen.hospital.service.AdministrativeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdministrativeServiceImpl implements AdministrativeService {

    @Autowired
    private AdministrativeMapper administrativeMapper;

    /**
     * 查询所有医生
     * @return
     */
    @Override
    public List<Administrative> getAll() {
        return administrativeMapper.getAll();
    }

    //条件查询
    @Override
    public PageInfo<Administrative> search(Params params) {
        //处理分页
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<Administrative> list = administrativeMapper.findBySearch(params);
        return PageInfo.of(list);
    }

    //添加
    @Override
    public void add(Administrative administrative) {
        //先查询数据库数据,如果此账号在数据库中已存在，则不能添加
        if (administrativeMapper.findByName(administrative.getDepartmentName()) != null) {
            throw new CustomException("此科室已存在,无法添加!");
        }
        administrativeMapper.add(administrative);
    }

    //修改
    @Override
    public void update(Administrative administrative) {
        administrativeMapper.update(administrative);
    }

    //根据id查询
    @Override
    public Administrative findById(String id) {
        Administrative administrative = administrativeMapper.findById(id);
        return administrative;
    }

    //删除
    @Override
    public void delete(String id) {
        administrativeMapper.delete(id);
    }

    //批量删除
    @Override
    public void batchDelete(List<Administrative> administratives) {
//        administratives.forEach(administrative -> {
//            administrativeMapper.delete(administrative.getAccount());
//        });
    }
}
